/*  
    Copyright (C) <2007-2013>  <Kay Diefenthal>

    ArgusTv.UI.MediaCenter is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ArgusTv.UI.MediaCenter is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ArgusTv.UI.MediaCenter.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using ArgusTV.ServiceAgents;
using ArgusTV.UI.MediaCenter.Logging;
using ArgusTV.UI.MediaCenter.Properties;
using ArgusTV.UI.MediaCenter.ViewModels;
using Microsoft.MediaCenter.Hosting;

namespace ArgusTV.UI.MediaCenter
{
    public class AddIn : IAddInModule, IAddInEntryPoint
    {
        private readonly ServerSettings _serverSettings = new ServerSettings();
        private Dictionary<string, object> _appInfo;
        private Dictionary<string, object> _entryPointInfo;

        public void Launch(AddInHost host)
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Settings.Default.Language);
            if ((string.IsNullOrEmpty(Settings.Default.ServerName)))
            {
                try
                {
                    if (String.Compare((string) _appInfo["id"], "{d202f961-a3da-4664-b27e-429184bd52c4}", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        new SettingsModel(new HistoryOrientedPageSession(), host).GoToSettingsView();
                    }
                    if (String.Compare((string) _appInfo["id"], "{1DE252E0-FB04-4e17-9196-0FC97FAA23A5}", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        new SettingsModel(new HistoryOrientedPageSession(), host).GoToSettingsView();
                    }
                }
                catch (Exception exception)
                {
                    Logger.Error(string.Format("{0}{1}:{2}", "Launch", "IsNullOrEmpty", exception));
                }
            }

            if ((Settings.Default.ServerName != null))
            {
                try
                {
                    _serverSettings.Port = Settings.Default.ServerPort;
                    _serverSettings.ServerName = Settings.Default.ServerName;
                    _serverSettings.Transport = ServiceTransport.NetTcp;
                    if ((Settings.Default.ServerIpAddress != string.Empty) && (Settings.Default.ServerMacAddress != string.Empty))
                    {
                        _serverSettings.WakeOnLan.Enabled = true;
                        _serverSettings.WakeOnLan.IPAddress = Settings.Default.ServerIpAddress;
                        _serverSettings.WakeOnLan.MacAddresses = Settings.Default.ServerMacAddress;
                        _serverSettings.WakeOnLan.TimeoutSeconds = 15;
                    }
                    ServiceChannelFactories.Initialize(_serverSettings, true);
                }
                catch (Exception exception)
                {
                    Logger.Error(string.Format("{0}{1}:{2}", "Launch", "Settings.Default", exception));
                }
            }
            if (ServiceChannelFactories.IsInitialized)
            {
                try
                {
                    if (String.Compare((string) _appInfo["id"], "{d202f961-a3da-4664-b27e-429184bd52c4}", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (String.Compare((string) _entryPointInfo["id"], "{2C05F813-C978-4bd7-B27C-12FD0A2A4D7C}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new RecordingModel(new HistoryOrientedPageSession(), host, 0).GoToRecordingView();
                        }
                        if (String.Compare((string)_entryPointInfo["id"], "{34FCED57-FAB6-43bc-ABAF-0EA8E9856090}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new EpgModel(new HistoryOrientedPageSession(), host, 0).GoToMultiChannelEpgView();
                        }
                        if (String.Compare((string) _entryPointInfo["id"], "{a5090d0e-86be-43a2-953e-563101f3f6ba}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new LiveModel(new HistoryOrientedPageSession(), host,0).GoToLiveView();
                        }

                        if (String.Compare((string) _entryPointInfo["id"], "{4B4E9E57-4C8E-4e1e-AC86-4E4DD5012E02}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new SearchModel(new HistoryOrientedPageSession(), host, 0).GoToSearchOptionsView();
                        }

                        if (String.Compare((string) _entryPointInfo["id"], "{70A1834D-50A4-4c17-8339-B394E30EE452}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new SettingsModel(new HistoryOrientedPageSession(), host).GoToSettingsView();
                        }
                    }
                    if (String.Compare((string) _appInfo["id"], "{1DE252E0-FB04-4e17-9196-0FC97FAA23A5}", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        if (String.Compare((string) _entryPointInfo["id"], "{7072BBEC-AC8E-49bf-9C41-776F24A8A5C8}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new RecordingModel(new HistoryOrientedPageSession(), host, 1).GoToRecordingView();
                        }
                        if (String.Compare((string)_entryPointInfo["id"], "{2078D279-95C5-47f4-BE71-F04EE4085F3F}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new EpgModel(new HistoryOrientedPageSession(), host, 1).GoToMultiChannelEpgView();
                        }
                        if (String.Compare((string) _entryPointInfo["id"], "{47A73D0D-34F3-4d06-83D5-E7910EE3B75D}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new LiveModel(new HistoryOrientedPageSession(), host, 1).GoToLiveView();
                        }
                        if (String.Compare((string) _entryPointInfo["id"], "{3C677388-52E6-4cfe-97E5-4F3170EF67ED}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new SearchModel(new HistoryOrientedPageSession(), host, 1).GoToSearchOptionsView();
                        }
                        if (String.Compare((string) _entryPointInfo["id"], "{737FFF61-CBF2-4a86-8DBE-A2A853F98343}", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            new SettingsModel(new HistoryOrientedPageSession(), host).GoToSettingsView();
                        }
                    }
                }
                catch (Exception exception)
                {
                    Logger.Error(string.Format("{0}{1}:{2}", "Launch", "IsInitialized", exception));
                }
            }
        }

        public void Initialize(Dictionary<string, object> appInfo, Dictionary<string, object> entryPointInfo)
        {
            Logger.SetLogFilePath("ArgusTV.UI.MediaCenter\\AddIn.log", Settings.Default.LogLevel);
            _appInfo = appInfo;
            _entryPointInfo = entryPointInfo;
        }

        public void Uninitialize()
        {
        }
    }
}